package rtda

type JStack struct {
	maxSize uint
	size    uint
	_top    *JFrame //栈帧
}

func NewJStack(maxSize uint) *JStack {
	return &JStack{maxSize: maxSize}
}

// 栈帧压栈
func (self *JStack) push(frame *JFrame) {
	if self.size >= self.maxSize {
		panic("java.lang.StackOverFlowError")
	}
	if self._top != nil {
		frame.lower = self._top
	}
	self._top = frame
	self.size++
}

func (self *JStack) pop() *JFrame {
	if self._top == nil {
		panic("jvm stack is empty!")
	}
	top := self._top
	self._top = top.lower
	top.lower = nil
	self.size--
	return top
}

func (self *JStack) top() *JFrame {
	if self._top == nil {
		panic("jvm stack is empty!")
	}
	top := self._top
	return top
}

func (self *JStack) isEmpty() bool {
	return self._top == nil
}

func (self *JStack) clear() {
	for !self.isEmpty() {
		self.pop()
	}
}

func (self JStack) getFrames() []*JFrame {
	jframes := make([]*JFrame, self.size)
	var i = 0
	for jFrame := self._top; jFrame != nil; jFrame = jFrame.lower {
		jframes[i] = jFrame
		i++
	}
	return jframes
}
